package com.mz361.stdapi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mz361.common.utils.RedisUtil;
import com.mz361.modules.system.domain.City;
import com.mz361.modules.system.service.CMemberService;
import com.mz361.modules.system.service.CityService;
import com.mz361.modules.system.service.CompanyService;
import com.mz361.modules.system.service.PayOrderService;
import com.mz361.stdapi.domain.LargeDataResult;
import com.mz361.stdapi.service.LargeScreenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Tuple;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class LargeScreenServiceImpl implements LargeScreenService {
    @Autowired
    CompanyService companyService;
    @Autowired
    PayOrderService payOrderService;
    @Autowired
    CMemberService cMemberService;
    @Autowired
    CityService cityService;
    @Autowired
    RedisUtil redisUtil;

    @Override
    public LargeDataResult largeScreen() {
        LargeDataResult largeDataResult = new LargeDataResult();
        largeDataResult.setCompanyTotal(companyService.countAll());
        largeDataResult.setUserTotal(cMemberService.countAll());
        largeDataResult.setOrderTotal(payOrderService.getCountOrder());
//        String[] areaData = {"海门", "鄂尔多斯", "招远", "舟山", "齐齐哈尔", "盐城", "赤峰", "青岛", "乳山", "金昌", "泉州", "莱西", "日照", "胶南", "南通", "拉萨", "云浮", "梅州", "文登", "上海", "攀枝花", "威海", "承德", "厦门", "汕尾", "潮州", "丹东", "太仓", "曲靖", "烟台", "福州", "瓦房店", "即墨", "抚顺", "玉溪", "张家口", "阳泉", "莱州", "湖州", "汕头", "昆山", "宁波", "湛江", "揭阳", "荣成", "连云港", "葫芦岛", "常熟", "东莞", "河源", "淮安", "泰州", "南宁", "营口", "惠州", "江阴", "蓬莱", "韶关", "嘉峪关", "广州", "延安", "太原", "清远", "中山", "昆明", "寿光", "盘锦", "长治", "深圳", "珠海", "宿迁", "咸阳", "铜川", "平度", "佛山", "海口", "江门", "章丘", "肇庆", "大连", "临汾", "吴江", "石嘴山", "沈阳", "苏州", "茂名", "嘉兴", "长春", "胶州", "银川", "张家港", "三门峡", "锦州", "南昌", "柳州", "三亚", "自贡", "吉林", "阳江", "泸州", "西宁", "宜宾", "呼和浩特", "成都", "大同", "镇江", "桂林", "张家界", "宜兴", "北海", "西安", "金坛", "东营", "牡丹江", "遵义", "绍兴", "扬州", "常州", "潍坊", "重庆", "台州", "南京", "滨州", "贵阳", "无锡", "本溪", "克拉玛依", "渭南", "马鞍山", "宝鸡", "焦作", "句容", "北京", "徐州", "衡水", "包头", "绵阳", "乌鲁木齐", "枣庄", "杭州", "淄博", "鞍山", "溧阳", "库尔勒", "安阳", "开封", "济南", "德阳", "温州", "九江", "邯郸", "临安", "兰州", "沧州", "临沂", "南充", "天津", "富阳", "泰安", "诸暨", "郑州", "哈尔滨", "聊城", "芜湖", "唐山", "平顶山", "邢台", "德州", "济宁", "荆州", "宜昌", "义乌", "丽水", "洛阳", "秦皇岛", "株洲", "石家庄", "莱芜", "常德", "保定", "湘潭", "金华", "岳阳", "长沙", "衢州", "廊坊", "菏泽", "合肥", "武汉", "大庆"};
//        1、企业区域分布
        Map<String, Long> areaCount = companyService.countCompanyGroupByAreaCode();
        if (!areaCount.isEmpty()) {
            Byte level = 1;
            JSONArray jsonArray = new JSONArray();
            List<City> cityList = cityService.getListByLevel(level);
            cityList.forEach(city -> {
                areaCount.forEach((k, v) -> {
                    if (null != city.getAreacode() && k.startsWith(city.getAreacode().toString())) {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("name", city.getCityName());
                        Object[] data = {city.getLongitude(), city.getLatitude(), v};
                        jsonObject.put("value", data);
                        jsonArray.add(jsonObject);
                    }
                });
            });
            largeDataResult.setAreaArray(jsonArray);
        }


        //SORT:STANDARDINFO:READONLINE  在线阅读排名
        Set<Tuple> readOnline = redisUtil.getTopKeywords("SORT:STANDARDINFO:READONLINE", 9);

        largeDataResult.setReadOnline(readOnline);
        //SORT:STANDARDINFO:PRINT   打印排名
        Set<Tuple> print = redisUtil.getTopKeywords("SORT:STANDARDINFO:PRINT", 9);

        largeDataResult.setPrint(print);
        //SORT:STANDARDINFO:DOWNLOAD 下载排名
        Set<Tuple> download = redisUtil.getTopKeywords("SORT:STANDARDINFO:DOWNLOAD", 9);

        largeDataResult.setDownload(download);
        //SORT:STANDARDINFO:TITLE 标准概要查看排名
        Set<Tuple> info = redisUtil.getTopKeywords("SORT:STANDARDINFO:TITLE", 9);

        largeDataResult.setInfo(info);
        //SORT:STANDARDSEARCH:TITLE 标准搜索标题排名
        Set<Tuple> searchInfo = redisUtil.getTopKeywords("SORT:STANDARDSEARCH:TITLE", 9);

        largeDataResult.setSearchInfo(searchInfo);
        //SORT:STANDARDSEARCH:KEYWORD 标准搜索关键字排名
        Set<Tuple> keywords = redisUtil.getTopKeywords("SORT:STANDARDSEARCH:KEYWORD", 9);
        largeDataResult.setKeywords(keywords);
        // 标准搜索国标分类排名
        Set<Tuple> GBCode = redisUtil.getTopKeywords("SORT:STANDARDSEARCH:CLASSCODE:GB", 9);
        largeDataResult.setGbCode(GBCode);
        // 标准搜索行标分类排名
        Set<Tuple> QTCode = redisUtil.getTopKeywords("SORT:STANDARDSEARCH:CLASSCODE:QT", 9);
        largeDataResult.setQtCode(QTCode);
        return largeDataResult;

    }


}
